#ifndef _GUI_MANAGER_H_
#define _GUI_MANAGER_H_

#include <sfFlashControl.h>
#include <boost/function.hpp>
#include <boost/unordered_map.hpp>
#include <boost/intrusive/list.hpp>
#include <SFML/Graphics.hpp>
#include "GUIControl.h"

class GUIManager
{
public:
	typedef boost::function<SwfUI::Argument(GUIControl*,const SwfUI::ArgumentList&)> ExternalFunction;
	GUIManager(const sf::Input& input);
	~GUIManager();

	static GUIManager& getInstance() { return *instance;}

	bool processEvent(const sf::Event& ev);
	GUIControl* createControl(int width, int height);
	void removeControl(GUIControl* control);
	void render(sf::RenderTarget& target) const;
	void bringToFront(GUIControl* control);
	void sendToBack(GUIControl* control);
	GUIControl* findControlAt(int x, int y);
	void addFunction(const std::wstring& functionName, const ExternalFunction& function);
	void removeFunction(const std::wstring& functionName);
	SwfUI::Argument callFunction(const std::wstring& functionName, GUIControl* caller, const SwfUI::ArgumentList& args);

private:
	bool mouseDown(const sf::Event& ev);
	bool mouseUp(const sf::Event& ev);
	bool mouseMoved(const sf::Event& ev);
	bool mouseWheel(const sf::Event& ev);

	static GUIManager* instance;
	typedef boost::intrusive::list<GUIControl, boost::intrusive::constant_time_size<false> > ControlCollection;
	mutable ControlCollection controls;
	typedef boost::unordered_map<std::wstring, ExternalFunction> FunctionCollection;
	FunctionCollection externalFunctions;
	const sf::Input& input;
	int oldX,oldY;
	bool dragging;
};

#endif